package Class::Value::SemanticAdapter;

use strict;
use warnings;
use UNIVERSAL::require;


our $VERSION = '0.02';


use base qw(Class::Value);


# Default; subclasses can redefine this. But it makes sense to keep the
# Data::Domain::* and Data::Semantic::* namespaces in sync.

sub semantic_class_name {
    my $self = shift;
    (my $semantic_class_name = ref $self) =~
        s/^Class::Value::/Data::Semantic::/;
    $semantic_class_name;
}


# FIXME might cache the adaptee unless $self->dirty;

sub adaptee {
    my $self = shift;
    my $semantic_class_name = $self->semantic_class_name;
    $semantic_class_name->require;
    $semantic_class_name->new($self->semantic_args);
}


# Return those of the value object's attributes that are relevant to the
# semantic data object constructor.

sub semantic_args { () }


sub is_valid_value {
    my ($self, $value) = @_;
    $self->adaptee->is_valid($value);
}


sub normalize_value {
    my ($self, $value) = @_;
    $self->adaptee->normalize($value);
}


sub is_valid_normalized_value {
    my ($self, $normalized) = @_;
    $self->adaptee->is_valid_normalized_value($normalized);
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - Adapter for Data::Semantic objects

=head1 SYNOPSIS

    {% p.package %}->new;

=head1 DESCRIPTION

This class is an adapter (wrapper) that turns L<Data::Semantic> objects into
L<Class::Value> objects.

=head1 METHODS

=over 4

{% p.write_methods %}

=item semantic_class_name

Returns the corresponding semantic class name. This method provides a default
mapping, the idea of which is to mirror the layout of the Data::Semantic class
tree. If you have a different mapping, override this method in a subclass.

So in the Class::Value::URI::http class, it will return
C<Data::Semantic::URI::http>.

=item adaptee

Takes the results of C<semantic_class_name()> and C<semantic_args()>, loads
the semantic data class and returns a semantic data object with the given args
passed to its constructor.

=item semantic_args

Return those of the value object's attributes, in hash format, that are
relevant to the semantic data object constructor.

=item is_valid_value

Like the same method in L<Class::Value>, but forwards the question to the
adapted data semantic object.

=item normalize_value

Like the same method in L<Class::Value>, but forwards the question to the
adapted data semantic object.

=item is_valid_normalized_value

Like the same method in L<Class::Value>, but forwards the question to the
adapted data semantic object.

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

